route( controller ) c#

114

route( controller ) c# -

By using the new [controller] token in your attribute routes you can ensure 
that the controller name in the route, is kept in sync with the name of the 
controller class. In the example below, [controller] will always be expanded 
to the name of the controller regardless of any future refactorings – in this
case it will be Hello.

[Route("[controller]")] // there are placeholders for common patterns 
                           as [area], [controller], [action], etc.

[HttpGet("")] // empty is valid. url.com/prefix

[Route("")] // empty is valid. url.com/

[HttpGet("/otherprefix/name")] // starting with / won't use the route prefix

[HttpGet("name/{id}")]
public IActionResult Index(int id){ ... // id will bind from route param.

[HttpGet("{id:int:required}")] // you can add some simple matching rules too.

Comments

Submit
0 Comments